package com.xiao.permission_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiao.permission_system.entity.Permission;
import com.xiao.permission_system.entity.Role;
import com.xiao.permission_system.mapper.PermissionMapper;
import com.xiao.permission_system.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author beth
 * @since 2019-10-26
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

         @Autowired
         private PermissionMapper  iPermissionMapper;

        /**
         * <p>
         * 插入一条记录
         * </p>
         *
         * @param entity 实体对象
         * @return 插入成功记录数
         */
        public int insert(Permission entity) {
           Integer result = iPermissionMapper.insert(entity);
           return result;
        }

        /**
         * <p>
         * 根据 ID 删除
         * </p>
         *
         * @param id 主键ID
         * @return 删除成功记录数
         */
          public int deleteById(String id) {
              Integer result = iPermissionMapper.deleteById(id);
              return result;
          }

          /**
           * <p>
           * 根据 ID 查询
           * </p>
           *
           * @param id 主键ID
           * @return 实体
           */
           public  Permission  selectById(String id){
              Permission  result = iPermissionMapper.selectById(id);
              return result;
           }

           /**
            * <p>
            * 根据 ID 修改
            * </p>
            *
            * @param entity 实体对象
            * @return 修改成功记录数
            */
            public boolean updateById(Permission entity) {
              int result = iPermissionMapper.updateById(entity);
              if(result>0){
                 return  true;
               }
               return false;
            }

             /**
              * 分页查询
              * @param page 分页信息
              * @param wrapper 查询条件
              * @return
              */
              public  IPage<Permission> selectPage(Page<Permission> page, @Param(Constants.WRAPPER) Wrapper<Permission> wrapper) {
                 return iPermissionMapper.selectPage(page,wrapper);
              }

            private Map<String, Collection<ConfigAttribute>> permissionMap = null;

            @PostConstruct
            /**
             * 从数据库中获取所有权限信息，然后进行遍历，存储到permissionMap集合中
             */
            public void initPermissions() {
                permissionMap = new HashMap<>();
                List<Permission> permissions = iPermissionMapper.selectList(null);
                for (Permission p : permissions) {
                    List<Role> roles = iPermissionMapper.getRoles(p.getId());
                    Collection<ConfigAttribute> collection = new ArrayList<ConfigAttribute>();
                    for (Role role : roles) {
                        ConfigAttribute configAttribute = new SecurityConfig("ROLE_" + role.getName());
                        collection.add(configAttribute);
                    }
                    permissionMap.put(p.getUrl(), collection);
                }
                System.out.println(permissionMap);
            }

            @Override
            public Map<String, Collection<ConfigAttribute>> getPermissionMap() {
                if (permissionMap == null || permissionMap.size() == 0) {
                    initPermissions();
                }
                return permissionMap;
            }

}
